<?php
class PeibeiAction extends Action {
	/*
	 * 配备模块
		users	登录后无权限可见
		anonymous 匿名无权限也可见
	*/
    public function _initialize(){
		$actions = array(
			'users'=>array(),
			'anonymous'=>array()
		);
		B('Authenticate', $actions);
	}
	
	public function view(){
		$uid=$_GET['uid'];
		$d_peibei=D('Peibei');
		$check=$d_peibei->check($uid);
//		dump($check);exit();
		if(empty($uid) || !$check){
			alert('error','员工配备信息不存在',U('hrm/peibei/add'));	
		}else{
			$get_peibei=$d_peibei->view($uid);
			foreach($get_peibei["peibei"] as $key=>$vals){
				switch($vals){
					case 1:
						$vsa[$key]="箱子";
						break;	
					case 2:
						$vsa[$key]="腰包";
						break;
					case 3:
						$vsa[$key]="工服";
						break;	
					case 4:
						$vsa[$key]="工牌";
						break;	
					case 5:
						$vsa[$key]="雨衣";
						break;	
					case 6:
						$vsa[$key]="Polo衫";
						break;	
					case 7:
						$vsa[$key]="头盔";
						break;	
				}
			}
			$get_peibei["peibei"]= implode(',',$vsa);
			$get_peibei['uname']=$d_peibei->Get_user($uid);
//			dump($get_peibei);exit();
			$this->assign('peibei', $get_peibei);
			$this->alert = parseAlert();
			$this->display();
		}
	}
	
	public function add(){
		if($this->isPost()){
			if(empty($_POST['user_id'])){
				alert('error','请选择员工再提交',U('hrm/peibei/add'));	
			}
			if(empty($_POST['peibei'])){
				alert('error','请选择配备信息再提交',U('hrm/peibei/add'));
			}
			$data['user_id']=$_POST['user_id'];
			$data['peibei']=json_encode($_POST['peibei']);
			$data['remark']=$this->_post('remark','tirm','');
			$data['creattime']=time();
			$data['add_user']=session('user_id');
			$addp=D("Peibei");
			if(!$addp->add($data)){
				alert('error','员工配备信息已存在！',U('hrm/peibei/add'));
			}else{
				alert('success','成功添加员工配备信息',U('hrm/peibei/li'));
			}
		}else{
			$this->alert = parseAlert();
			$this->display();
		}
	}
	
	//员工配备列表
	public function li(){
		$p = $this->_get('p','intval',1);
		$status = $this->_get('status','trim','1');
		$did=$this->_get('did','intval','');
		
		$gli=D('Peibei');
		$d_show=$gli->Gpage($p,$status,$did);
		
		foreach($d_show['list'] as $k=>$val){
			$glist[$k]=$val;
			foreach(json_decode($val["peibei"]) as $key=>$vals){
				switch($vals){
					case 1:
						$vsa[$k][$key]="箱子";
						break;	
					case 2:
						$vsa[$k][$key]="腰包";
						break;
					case 3:
						$vsa[$k][$key]="工服";
						break;	
					case 4:
						$vsa[$k][$key]="工牌";
						break;	
					case 5:
						$vsa[$k][$key]="雨衣";
						break;	
					case 6:
						$vsa[$k][$key]="Polo衫";
						break;	
					case 7:
						$vsa[$k][$key]="头盔";
						break;	
				}
			}
			
			$glist[$k]['uname']=$gli->Get_user($val['user_id']);
			$glist[$k]['add_user']=$gli->Get_user($val['add_user']);
			$glist[$k]["peibei"]= implode(',',$vsa[$k]);

		}
		$dlist=$gli->getDepartmentList(13,'&nbsp;-');

//		dump($d_show['list']);exit();
		
		$this->assign('dlist', $dlist);
		$this->assign('glist', $glist);
		$this->assign('page', $d_show['page']);
		$this->alert = parseAlert();
		$this->display();
	}
	
	//编辑
	public function edit(){
		$ginfo=D('Peibei');
		if($_POST){
			$status["pid"]=$_POST["pid"];
			$status["user_id"]=$_POST["user_id"];
			$edata["peibei"]=json_encode($_POST["peibei"]);
			$edata["remark"]=$_POST["remark"];
			$edata["update"]=time();
			
			$upthis=$ginfo->update($edata,$status);
			if($upthis){
				alert('success','成功更新配备信息',U('hrm/peibei/li'));
			}else{
				if(empty($_GET['id'])){
					alert('error','员工配备信息不存在！',U('hrm/peibei/li'));
				}
				alert('error','员工更新信息失败，请联系管理员！',U('hrm/peibei/edit','id='.$_POST["pid"]));
			}
//			dump($edata);exit();
		}
		
		
		
		$ginfos=$ginfo->show($_GET['id']);
		
		$ginfos['uname']=$ginfo->Get_user($ginfos['user_id']);
		
		$peibei=array(
			"1"=>'箱子',
			"2"=>'腰包',
			'3'=>'工服',
			'4'=>'工牌',
			'5'=>'雨衣',
			'6'=>'Polo衫',
			'7'=>'头盔'
		);
		

		$this->assign('pbinfo', $ginfos['peibei']);
		$this->assign('peibei', $peibei);
		$this->assign('uinfo', $ginfos);
		$this->alert = parseAlert();
		$this->display();
	}
	
	//删除
	public function del(){
		$pid=$_GET['id'];
		if(empty($pid)){
			alert('error','员工配备信息不存在！',U('hrm/peibei/li'));
		}
		$ginfo=D('Peibei');
		$del=$ginfo->delete($pid);
		if($del){
			alert('success','成功删除配备信息',U('hrm/peibei/li'));
		}else{
			alert('error','信息删除失败，请联系管理员！！',U('hrm/peibei/li'));
		}
	}
}